| 1 | import {
|
| 2 | View,
|
| 3 | StyleSheet,
|
| 4 | KeyboardAvoidingView,
|
| 5 | ScrollView,
|
| 6 | ActivityIndicator,
|
| 7 | } from "react-native";
|
| 8 | import { Text } from "@/components/ui/Text";
|
| 9 | import { useState } from "react";
|
| 10 | import { router, useLocalSearchParams, Stack } from "expo-router";
|
| 11 |
|
| 12 | import { Card } from "@/components/ui/Cards";
|
| 13 | import { TextArea } from "@/components/ui/Input";
|
| 14 | import { Button } from "@/components/ui/Button";
|
| 15 | import { Section, SectionHeader } from "@/components/ui/Sections";
|
| 16 | import { Spacing, Typography, Colors, BorderRadius } from "@/constants/theme";
|
| 17 | import { Ionicons, MaterialIcons } from "@expo/vector-icons";
|
| 18 | import { useUpdateTrigger, useTrigger } from "@/hooks";
|
| 19 | import { CategoryChips, CategoryOption } from "@/components/ui/CategoryChips";
|
| 20 | import { TriggerCategory } from "@/constants/triggers";
|
| 21 | import { TRIGGER_CATEGORY_DEFINITIONS } from "@/constants/trigger-categories";
|
| 22 | import { TimePicker } from "@/components/ui/TimePicker";
|
| 23 | import type { Trigger } from "@/lib/api";
|
| 24 |
|
| 25 | const TRIGGER_CATEGORIES: CategoryOption<TriggerCategory>[] =
|
| 26 | TRIGGER_CATEGORY_DEFINITIONS.map((d) => ({
|
| 27 | label: d.label,
|
| 28 | value: d.id,
|
| 29 | icon: <MaterialIcons size={16} color={d.color} name={d.icon} />,
|
| 30 | }));
|
| 31 |
|
| 32 | export default function EditTrigger() {
|
| 33 | const { id } = useLocalSearchParams<{ id: string }>();
|
| 34 | const { data: trigger, isLoading } = useTrigger(id);
|
| 35 |
|
| 36 | if (isLoading) {
|
| 37 | return (
|
| 38 | <View style={styles.centered}>
|
| 39 | <ActivityIndicator size="large" color={Colors.light.tint} />
|
| 40 | </View>
|
| 41 | );
|
| 42 | }
|
| 43 |
|
| 44 | if (!trigger) {
|
| 45 | return null;
|
| 46 | }
|
| 47 |
|
| 48 | return <EditTriggerForm id={id} trigger={trigger} />;
|
| 49 | }
|
| 50 |
|
| 51 | function EditTriggerForm({ id, trigger }: { id: string; trigger: Trigger }) {
|
| 52 | const updateTrigger = useUpdateTrigger();
|
| 53 |
|
| 54 | const [moment, setMoment] = useState(new Date(trigger.moment));
|
| 55 | const [comment, setComment] = useState(trigger.comment || "");
|
| 56 | const [category, setCategory] = useState(trigger.category);
|
| 57 |
|
| 58 | const save = async () => {
|
| 59 | const data = {
|
| 60 | moment,
|
| 61 | comment,
|
| 62 | category,
|
| 63 | };
|
| 64 |
|
| 65 | await updateTrigger.mutateAsync({ id: String(id), payload: data });
|
| 66 | router.back();
|
| 67 | };
|
| 68 |
|
| 69 | return (
|
| 70 | <KeyboardAvoidingView behavior="height" style={styles.keyboardView}>
|
| 71 | <Stack.Screen options={{ title: "Editar Registro" }} />
|
| 72 | <ScrollView scrollEventThrottle={16}>
|
| 73 | <View style={styles.container}>
|
| 74 | <View style={styles.header}>
|
| 75 | <View style={styles.innerHeaderWrapper}>
|
| 76 | <View style={[styles.headerIcon, { backgroundColor: "#FFF7ED" }]}>
|
| 77 | <Ionicons name="flash-outline" size={20} color="#EA580C" />
|
| 78 | </View>
|
| 79 |
|
| 80 | <View style={styles.headerTitleSubtitle}>
|
| 81 | <Text style={styles.headerTitle}>Editar Gatilho</Text>
|
| 82 | <Text style={styles.headerSubtitle}>
|
| 83 | Atualize os dados do acontecimento
|
| 84 | </Text>
|
| 85 | </View>
|
| 86 | </View>
|
| 87 |
|
| 88 | <Text style={styles.description}>
|
| 89 | Você pode editar este registro nos primeiros 5 minutos após a
|
| 90 | criação.
|
| 91 | </Text>
|
| 92 | </View>
|
| 93 |
|
| 94 | <Section>
|
| 95 | <SectionHeader title="Horário do Evento" />
|
| 96 | <TimePicker value={moment} onChange={setMoment} />
|
| 97 | </Section>
|
| 98 |
|
| 99 | <Section>
|
| 100 | <SectionHeader title="Selecionar uma categoria" />
|
| 101 | <CategoryChips
|
| 102 | options={TRIGGER_CATEGORIES}
|
| 103 | active={category}
|
| 104 | onChange={setCategory}
|
| 105 | />
|
| 106 | </Section>
|
| 107 |
|
| 108 | <Section>
|
| 109 | <SectionHeader title="Adicione detalhes" />
|
| 110 |
|
| 111 | <Card style={styles.mainCard}>
|
| 112 | <TextArea
|
| 113 | label="Comentários sobre"
|
| 114 | variant="darkGhost"
|
| 115 | onChangeText={(val) => setComment(val)}
|
| 116 | value={comment}
|
| 117 | minRows={5}
|
| 118 | maxRows={10}
|
| 119 | placeholder="Descreva o que gerou esse sentimento"
|
| 120 | />
|
| 121 | </Card>
|
| 122 | </Section>
|
| 123 |
|
| 124 | <Button
|
| 125 | title="Salvar Alterações"
|
| 126 | onPress={save}
|
| 127 | loading={updateTrigger.isPending}
|
| 128 | disabled={updateTrigger.isPending}
|
| 129 | />
|
| 130 | </View>
|
| 131 | </ScrollView>
|
| 132 | </KeyboardAvoidingView>
|
| 133 | );
|
| 134 | }
|
| 135 |
|
| 136 | const styles = StyleSheet.create({
|
| 137 | container: {
|
| 138 | gap: 24,
|
| 139 | paddingHorizontal: Spacing.containerPadding,
|
| 140 | paddingVertical: Spacing.sectionGap,
|
| 141 | marginBottom: 50,
|
| 142 | },
|
| 143 | mainCard: {
|
| 144 | padding: Spacing.cardGap,
|
| 145 | },
|
| 146 | header: {},
|
| 147 | headerIcon: {
|
| 148 | paddingVertical: 15,
|
| 149 | paddingHorizontal: 14,
|
| 150 | width: 48,
|
| 151 | borderRadius: BorderRadius.md,
|
| 152 | },
|
| 153 | innerHeaderWrapper: {
|
| 154 | flex: 1,
|
| 155 | flexDirection: "row",
|
| 156 | alignItems: "center",
|
| 157 | },
|
| 158 | headerTitleSubtitle: {
|
| 159 | marginLeft: 10,
|
| 160 | },
|
| 161 | headerTitle: {
|
| 162 | ...Typography.headlineMd,
|
| 163 | },
|
| 164 | headerSubtitle: {
|
| 165 | ...Typography.bodyMd,
|
| 166 | color: Colors.light.textSecondary,
|
| 167 | },
|
| 168 | description: {
|
| 169 | ...Typography.bodyMd,
|
| 170 | color: Colors.light.textSecondary,
|
| 171 | marginTop: 20,
|
| 172 | },
|
| 173 | keyboardView: {
|
| 174 | flex: 1,
|
| 175 | },
|
| 176 | centered: {
|
| 177 | flex: 1,
|
| 178 | justifyContent: "center",
|
| 179 | alignItems: "center",
|
| 180 | },
|
| 181 | });
|